<?php
namespace App\Http\Controllers\AppApi\Home;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\JobsCompanysModel;
use App\Models\User\UserModel;
use App\Repositories\Admin\JobCompanyRepository;
use App\Repositories\Admin\JobStaffRepository;
use Illuminate\Http\Request;

class RecruitController extends BaseController
{
    /**
     * 招聘流程
     * 制作者：九哥
     */

    protected $model;
    protected $jobStaff;

    public function __construct()
    {
        parent::__construct();
        $this->model = new JobCompanyRepository();
        $this->jobStaff = new JobStaffRepository();
    }

    //////api/home/recruit/index
    public function getIndexList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $page = isset($request->page) ? $request->page : 0;
       /* if (!$token) {
            return $this->toJson(-2);
        }*/
        /*$uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }*/
        $resData = \Illuminate\Support\Facades\Cache::remember(
            'api/home/recruit/index'.$page, 2,
            function()use($page) {
              ///  echo "s*************";
            return  $this->model->getAppJobList($page);
        });
        if (!$resData) {
            return $this->toJson(-3);
        }
        return $this->toJson(0,$resData);
    }

    /**
     * 通过CID，获取某个企业的招聘岗位
     */
    public function getIndexByCid(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
       // if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $cid = $request->cid;
        $type = $request->type; //1工人，2企业
        $cid = $type==2 ? $uid : $cid;
//        return $this->toJson(-1,$cid);
        $resData = $this->model->getAppJobsByCid($cid);
        if (!$resData) { return $this->toJson(-3); }
        return $this->toJson(0,$resData);
    }

//    public function setAdd(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        $token = $request->token;
//        $res = $request->all();
//        if (!$token) {
//            return $this->toJson(-2);
//        }
//        $uid = $this->accessGet($token);
//        if (!$uid) { return $this->toJson(-13); }
//        $jobComs = array(
//            'cid' => $uid,
//            'job' => $res['job'],
//            'num' => $res['num'],
//            'emolument' => $res['emolument'],//薪酬
//            'welfare' => $res['welfare'],//福利待遇
//            'rest' => $res['rest'], //休息制度
//            'education' => $res['education'], //学历
//            'work_year' => $res['work_year'], //工作年限
//            'address' => $res['address'], //工作地点
//            'detail' => $res['detail'], //岗位类型
//            'content' => $res['content'], //工作内容
//            'work_hour' => $res['work_hour'], //'工作时间'
//            'contact_name' => $res['contact_name'], //'联系人姓名
//            'contact_telephone' => $res['contact_telephone'], //联系人号码
//            'email' => $res['email'], //邮箱
//            'sex' => $res['sex'],//性别
//            'age_range' => $res['age_range'],//年龄区间
//            'skill' => $res['skill'],//技术工种
//        );
//        foreach ($jobComs as $jobCom) {
//            if (!$jobCom) { return $this->toJson(-2); }
//        }
//        $lastId = $this->model->setAdd($jobCom);
//        $dataArr = array(
//            'id' => $lastId,
//            'cid' => $cid,
//        );
//        return $this->toJson(0,$dataArr);
//    }

    /**
     * 获取岗位详情
     * /api/home/recruit/show
     */
    public function getShow(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }

        $uid = $this->accessGet();
        if (!$uid) { return $this->toJson(-13); }
        $id = $request->job;
        $model = $this->model->getAppShowOne($id);

        if(!$model){
            //return $this->toJson(1,[],'该岗位已经下架');
            OA::Json(1,'该岗位已经下架');
        }
        if($model->status!=JobsCompanysModel::status_fabuzhong&&$model->cid!=$uid){
            OA::Json(1,'该岗位已经下架');
        }
        $model['telephone']=$model->contact_telephone;

        if($model['head']==''){
            $userinfo=UserModel::getinfo($model['cid']);
            $model['head']=Fun::pImg($userinfo['head']);
        }
      //  var_dump($model->welfare);exit;
        if($model->welfare){
            $model->welfare=$this->model->getWelfares($model->welfare);
        }else{
            $model->welfare='暂无';
        }
        $myuser=UserModel::getinfo($uid);
        if($myuser&&$myuser['is_yingpin']=='1'){
            $model['yunxuyingpin']=true;
        }else{
            $model['yunxuyingpin']=false;
        }
        //$list=$model->toArry();
        //
        if (!$model) { return $this->toJson(-3); }
        return $this->toJson(0,$model);
    }

    /**
     * 工人判断、获取简历
     */
    public function getResume(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
       /* if (!$token) {
            return $this->toJson(-2);
        }*/
        $cid = $this->accessGet($token);
        if (!$cid) { return $this->toJson(-13); }
        $genre = $request->genre; //1工人获取，2企业获取
        $genre = $genre==1 ? 1 : 2;
        $uid = $request->uid;
        $id = $request->job;
        $conditions = array(
            'uid' => $uid,
            'cid' => $cid,
            'jobId' => $id,
            'genre' => $genre,
        );
        $resJobStaff = $this->jobStaff->getAppApply($conditions);
        if (!$resJobStaff) {
            return $this->toJson(-3);
        }
        return $this->toJson(0,$resJobStaff);
    }

    /**
     * 工人发送简历
     * /api/home/recruit/resume-add
     */
    public function sendResume(Request $request)
    {


        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        /*$token = $request->token;
        if (!$token) {
            return $this->toJson(-2);
        }*/
        $uid = $this->accessGet();
        if (!$uid) { return $this->toJson(-13); }
       /// $genre = $request->genre; //1工人获取，2企业获取
     ///   $genre = $genre==1 ? 1 : 2;
        $cid = $request->cid;
        $id = $request->job;
        $conditions = array(
            'uid' => $uid,
            'cid' => $cid,
            'jobId' => $id,
          ///  'genre' => $genre,
        );

        $resJobStaff = $this->jobStaff->getAppApply($conditions);
        if ($resJobStaff) {
            return $this->toJson(-41);
        }

        $jobTalkId = $this->jobStaff->setAppApply($conditions);

        return $this->toJson(0,$jobTalkId);
    }

    /**
     * 工人/企业判断、获取面试邀请
     */
    public function getMeet(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        /*if (!$token) {
            return $this->toJson(-2);
        }*/
        $cid = $this->accessGet($token);
        if (!$cid) { return $this->toJson(-13); }
        $genre = $request->genre; //1工人获取，2企业获取
        $genre = $genre==1 ? 1 : 2;
        $uid = $request->uid;
        $id = $request->job;
        $conditions = array(
            'uid' => $uid,
            'cid' => $cid,
            'jobId' => $id,
            'genre' => $genre,
        );
        $resJobStaff = $this->jobStaff->getAppMeet($conditions);
        if (!$resJobStaff) {
            return $this->toJson(-3);
        }
        return $this->toJson(0,$resJobStaff);
    }

    /**
     * 企业发送面试邀请
     */
    public function setMeet(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        /*if (!$token) {
            return $this->toJson(-2);
        }*/
        $cid = $this->accessGet($token);
        if (!$cid) { return $this->toJson(-13); }
        $genre = $request->genre; //1工人获取，2企业获取
        $genre = $genre==1 ? 1 : 2;
        $uid = $request->uid;
        $id = $request->job;
        $conditions = array(
            'uid' => $uid,
            'cid' => $cid,
            'jobId' => $id,
            'genre' => $genre,
        );
        $resJobStaff = $this->jobStaff->getAppMeet($conditions);
        if (!$resJobStaff) {
            return $this->toJson(-3);
        }
        if (in_array($resJobStaff->status,[4,5]) ) {
            return $this->toJson(-44);
        }
        $this->jobStaff->setAppMeet($conditions);
        return $this->toJson(0,$resJobStaff);
    }
}